<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    title: {
      type: String,
      default: "",
    },
    chartData: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {},
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      let placeHolderStyle = {
        normal: {
          label: {
            show: false,
          },
          labelLine: {
            show: false,
          },
          color: "rgba(0,0,0,0)",
          borderWidth: 0,
        },
        emphasis: {
          color: "rgba(0,0,0,0)",
          borderWidth: 0,
        },
      };

      this.chart = echarts.init(this.$el);
      let _this = this;
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      let carTypeData = chartData.map((v) => v.carTypeName);
      let onlineData = chartData.map((v) => {
        return v.totalOnlineCar;
      });
      let offlineData = chartData.map((v) => {
        return v.totalOfflineCar;
      });

      var colorList = [
        "#ffc300",
        "#00ffff",

        "#6ecd8e",
        "#539d73",
        "#26bc96",
        "#2b7337",
        "#F19E34",
        "#9565F4",
        "#0255FD",
        "#7ED3F4",
        "#002E5A",
        "#07D2F9",
      ];

      let option = {
        color: colorList,
        tooltip: {
          trigger: "axis",
          axisPointer: {
            label: {
              show: true,
              backgroundColor: "#fff",
              color: "#556677",
              borderColor: "rgba(0,0,0,0)",
              shadowColor: "rgba(0,0,0,0)",
              shadowOffsetY: 0,
            },
            lineStyle: {
              width: 0,
            },
          },
          backgroundColor: "#fff",
          textStyle: {
            color: "#5c6c7c",
          },
          padding: [10, 10],
          extraCssText: "box-shadow: 1px 0 2px 0 rgba(255,255,255,0.5)",
        },

        grid: {
          left: "3%",
          right: "5%",
          bottom: "10%",
          top: "4%",
          containLabel: true,
        },
        legend: {
          icon: "circle",
          show: true,
          data: ["在线", "离线"],
          x: "center",
          y: "bottom",
          textStyle: { color: "#fffff" },
        },
        xAxis: {
          type: "category",
          data: carTypeData,
          // axisLabel: {
          //   color: "#fff",
          //   interval: 0,
          //   rotate: 30,
          // },
        },

        yAxis: [
          {
            axisLabel: {
              color: "#ffffff",
            },
            min: 0,

            minInterval: 1,
            // max: 140,
            splitNumber: 4,
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.1)",
              },
            },
            axisLine: {
              show: true,
            },
            axisLabel: {
              show: true,
              margin: 20,
              textStyle: {
                color: "#d1e6eb",
              },
            },
            axisTick: {
              show: true,
            },
          },
        ],
        // dataZoom: {
        //   xAxisIndex: [0, 1],
        // },
        series: [
          {
            name: "离线",
            type: "bar",
            stack: "辆",
            barMaxWidth: 10,
            data: offlineData,
            itemStyle: {
              normal: {
                barBorderRadius: 0,

                color: (params) => {
                  return "#ffc300";
                },
              },
            },
          },
          {
            name: "在线",
            type: "bar",
            stack: "辆",
            barMaxWidth: 10,
            data: onlineData,
            itemStyle: {
              normal: {
                barBorderRadius: 0,

                color: (params) => {
                  return "#00ffff";
                },
              },
            },
            label: {
              show: true,
              position: "top",
              color: "#00ffff",
              fontSize: 10,
            },
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>
